<template>
  <div>
    <ComDialog
      width="600"
      @EventClose="closeFn"
      :dialogVisible="dialogVisible"
      :title="isEdit ? '修改问金发放' : '新增问金发放'"
    >
      <el-form ref="form" :rules="rules" label-width="100px" :model="addForm">
        <el-form-item label="套餐版本" prop="version">
          <el-input v-model="addForm.version"></el-input>
        </el-form-item>
        <el-form-item label="原价" prop="amount">
          <el-input v-model.number="addForm.amount"></el-input>
        </el-form-item>
        <el-form-item label="销售价" prop="price">
          <el-input v-model.number="addForm.price"></el-input>
        </el-form-item>
        <el-form-item label="问金" prop="voucher">
          <el-input v-model.number="addForm.voucher"></el-input>
        </el-form-item>
        <el-form-item label="人数" prop="people">
          <el-input v-model.number="addForm.people"></el-input>
        </el-form-item>
        <el-form-item label="价格说明" prop="info">
          <el-input v-model="addForm.info"></el-input>
        </el-form-item>
        <el-form-item label="折扣" prop="discount">
          <el-input
            max="1"
            @input="inputFn"
            v-model="addForm.discount"
          ></el-input>
        </el-form-item>
        <el-form-item label="连续折扣" prop="discountYears">
          <el-input v-model="addForm.discountYears"></el-input>
        </el-form-item>
      </el-form>

      <template name="footer">
        <el-button @click="closeFn"> 取消 </el-button>
        <el-button
          type="primary"
          @click="saveAdditionFn"
          v-hasPermi="['financial:priceVip:edit']"
        >
          保存
        </el-button>
      </template>
    </ComDialog>
  </div>
</template>

<script>
import ComDialog from "../../../../components/comDialog.vue";
import {
  getPriceVipAddApi,
  getPriceVipGetInfoApi,
  getPriceVipEditApi,
} from "@/wss/api/financial/pricing/priceVip.js";
export default {
  props: {
    selectOption: {
      type: Array,
      default: () => [],
    },
    dialogVisible: {
      default: false,
      type: Boolean,
    },
  },
  components: {
    ComDialog,
    // 新增问金发放数据
  },
  data() {
    // 检测是否大于1
    var validateDiscount = (rule, value, callback) => {
      console.log(rule);
      if (parseFloat(value) <= 1) {
        callback();
      } else {
        callback(new Error("无效"));
      }
    };
    return {
      // 判断是否是修改
      isEdit: false,
      addForm: {
        version: "",
        amount: "",
        price: "",
        voucher: "",
        people: "",
        info: "",
        discount: "",
        discountYears: "",
      },
      options: [],
      // 检查规则
      rules: {
        discount: [{ validator: validateDiscount, trigger: "blur" }],
        discountYears: [{ validator: validateDiscount, trigger: "blur" }],
        info: [{ message: "无效", trigger: "blur", type: "string" }],
        version: [{ message: "无效", trigger: "blur", type: "string" }],
        amount: [{ message: "无效", trigger: "blur", type: "number" }],
        price: [{ message: "无效", trigger: "blur", type: "number" }],
        voucher: [{ message: "无效", trigger: "blur", type: "number" }],
        people: [{ message: "无效", trigger: "blur", type: "number" }],
      },
    };
  },

  created() {
    this.getPositionListFn();
  },
  methods: {
    // 根据id获取详细信息
    async getPriceVipGetInfoFn(row) {
      const { data, code } = await getPriceVipGetInfoApi({ id: row.idStr });
      if (code == 200) {
        this.addForm = data;
      }
    },

    // 输入时间
    inputFn(val) {},
    // 获取角色列表
    async getPositionListFn() {},
    // 根据id获取详细
    async getDetailFn(row) {},
    // 关闭对话框时初始化
    closeFn() {
      this.isEdit = false;
      (this.addForm = {
        postion: "",
        level: "",
        ratio: "",
      }),
        this.$emit("update:dialogVisible", false);
    },
    // 刷新父组件的list
    fretchList(msg, code) {
      this.closeFn();
      if (code === 200) {
        this.$modal.msgSuccess(msg);
        this.$emit("EventFretchList");
      }
    },
    // 新增或者修改
    async saveAdditionFn() {
      try {
        await this.$refs.form.validate();

        // 保留两位小数
        for (const key in this.addForm) {
          if (typeof this.addForm[key] == "number" && key in this.rules) {
            this.addForm[key] = this.addForm[key].toFixed(1);
          }
        }
        // 修改
        if (this.addForm.idStr) {
          this.addForm.id = this.addForm.idStr;
          let { msg, code } = await getPriceVipEditApi(this.addForm);
          this.fretchList(msg, code);
        } else {
          // 新增
          let { msg, code } = await getPriceVipAddApi(this.addForm);
          this.fretchList(msg, code);
        }
      } catch (error) {
        this.$modal.msgError("操作有问题");
      }

      //   console.log(this.addForm, "添加或者修改函数");
    },
  },
};
</script>

<style>
</style>